﻿using IRepositoryBll;
using IRepositoryDal;
using RepositoryEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryBll
{
    public class CategoryBll : ICategoryBll
    {
        private ICategoryDal _CategoryDal;
        public CategoryBll(ICategoryDal CategoryDal)
        {
            _CategoryDal = CategoryDal;
        }

        public bool AddCategory(Category category)
        {
            return _CategoryDal.Add(category);
        }

        public bool DeleteCategory(string categoryId)
        {
            Category CategoryId = _CategoryDal.GetEntity(categoryId);
            if (categoryId != null)
            {
                return _CategoryDal.Delete(categoryId);
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取类型ID集合
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public Category GetCategory(string categoryId)
        {
            return _CategoryDal.GetEntity(categoryId);
        }

        public object GetCategoryListByPage(int page, int limit, out int count, string categoryName)
        {
            //获取耗材信息集合
            var category = _CategoryDal.GetEntitiesDb().Select(u => new
            {
                u.ID,
                u.Description,
                u.CategoryName
            });
            if (!string.IsNullOrEmpty(categoryName))
            {
                category = category.Where(u => u.CategoryName.Contains(categoryName));

            }
            //统计数据数量
            count = category.Count();
            category = category.Skip((page - 1) * limit).Take(limit);
            var list = category.ToList().Select(u =>
            {
                return new
                {
                    u.ID,
                    u.Description,
                    u.CategoryName,

                };
            });

            return list;
        }


        /// <summary>
        /// 获取类型名称下拉选数据
        /// </summary>
        /// <returns></returns>
        public object GetCategoryOptions()
        {
            //linq
            var categoryName = _CategoryDal.GetEntitiesDb().Select(d => new
            {
                d.ID,
                d.CategoryName
            }).ToList();
            return categoryName;
        }

        /// <summary>
        /// 更新类型信息
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="CategoryName"></param>
        /// <param name="Description"></param>
        /// <returns></returns>
        public bool UpdateCategory(string categoryId, string categoryName, string description)
        {
            Category category = _CategoryDal.GetEntity(categoryId);
            if (category != null)
            {
                category.Description = description;
                category.CategoryName = categoryName;


                return _CategoryDal.Update(category);
            }
            else
            {
                return false;
            }


        }

        /// <summary>
        /// 更新类型
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public bool UpdateCategory(Category category)
        {
            return _CategoryDal.Update(category);
        }
    }
}
